﻿using System;
using System.Collections.Generic;
using System.Linq;
using TestOnlineProject.Models.Entities;

namespace TestOnlineProject.Models.DataAccess
{
    public class DiscussionDAO
    {
        TestOnlineDBEntities db = new TestOnlineDBEntities();

        /// <summary>
        /// Select All Discussion
        /// </summary>
        /// <returns>List Discussion</returns>

        public List<Discussion> Select()
        {
            try
            {
                var discussions = db.Discussions.ToList();
                return discussions;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Select List Discussions in Subject
        /// </summary>
        /// <param name="subjectId">Id of Subject</param>
        /// <returns>List Discussion</returns>

        public List<Discussion> SelectBySubjectId(int subjectId)
        {
            try
            {
                var discussions = db.Discussions.Where(x => x.SubjectId == subjectId).ToList();
                return discussions;
            }
            catch (Exception)
            {
                return null;
            }

        }

        /// <summary>
        /// Select Discussion
        /// </summary>
        /// <param name="discussionId">Id of Discussion</param>
        /// <returns>Discussion object</returns>

        public Discussion SelectByDiscussionId(int discussionId)
        {
            try
            {
                var discussion = db.Discussions.FirstOrDefault(x => x.DiscussionId == discussionId);
                return discussion;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Select List Replies of Discussion
        /// </summary>
        /// <param name="parentId">Id of Discussion</param>
        /// <returns>List Replies</returns>

        public List<Discussion> SelectByParentId(int parentId)
        {
            try
            {
                var replies = db.Discussions.Where(x => x.ParentId == parentId).ToList();
                return replies;
            }
            catch (Exception)
            {

                return null;
            }
        }

        /// <summary>
        /// Select List Discussion of User
        /// </summary>
        /// <param name="userId">Id of User</param>
        /// <returns>List Discussion</returns>

        public List<Discussion> SelectByUserId(int userId)
        {
            try
            {
                var discussions = db.Discussions.Where(x => x.UserId == userId).ToList();
                return discussions;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Insert new Discussion
        /// </summary>
        /// <param name="discussion">Discussion Object</param>
        /// <returns>Number of Discussion has been add</returns>

        public int Insert(Discussion discussion)
        {
            try
            {
                db.Discussions.AddObject(discussion);
                var result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                return 0;
            }

        }

        /// <summary>
        /// Update Discussion
        /// </summary>
        /// <param name="discussion">Discussion Object</param>
        /// <returns>Number of Discussion has been update</returns>

        public int Update(Discussion discussion)
        {
            try
            {
                var result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {

                return 0;
            }
        }

        /// <summary>
        /// Delete Discussion
        /// </summary>
        /// <param name="discussion">Discussion Object</param>
        /// <returns>Number of Discussion has been delete</returns>

        public int Delete(Discussion discussion)
        {
            try
            {
                discussion.Reports.ToList().ForEach(db.DeleteObject);

                db.Discussions.DeleteObject(discussion);
                var result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// <summary>
        /// Delete Discussion of User
        /// </summary>
        /// <param name="userId">Id of User</param>
        /// <returns>Number of Discussion has been deleted</returns>

        public int DeleteByUserId(int userId)
        {
            var result = 0;
            try
            {
                var discussions = db.Discussions.Where(x => x.UserId == userId & x.ParentId == null).ToList();

                foreach (var discussion in discussions)
                {
                    var replies = db.Discussions.Where(x => x.ParentId == discussion.DiscussionId).ToList();
                    foreach (var reply in replies)
                    {
                        db.Discussions.DeleteObject(reply);
                    }
                    db.Discussions.DeleteObject(discussion);
                    result += db.SaveChanges();
                }

                var other = db.Discussions.Where(x => x.UserId == userId & x.ParentId != null).ToList();
                foreach (var discussion in other)
                {
                    db.Discussions.DeleteObject(discussion);
                }
                result += db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                return 0;
            }
        }
    }
}